#ifndef DATASENDER_H
#define DATASENDER_H

#include <QObject>
#include <QUdpSocket>
#include <QHostAddress>
#include <QMutex>
#include <QElapsedTimer>
#include <QTimer>

class DataSender : public QObject
{
    Q_OBJECT
public:
    explicit DataSender(int width, int height, int fps, QObject *parent = nullptr);
    
    void setDestination(const QString &ip, quint16 port);
    void setDestination(const QHostAddress &address, quint16 port);

    void setBindIpAndPort(const QString &ip, quint16 port);
    
signals:
    void errorOccurred(const QString &errorString);

    void sigToShowUi(int width, int height, int fps); //在ui上显示
    
public slots:
    void onSendData(const QByteArray &data);

    void onInitUdp();
    
private:
    QUdpSocket *m_socket;
    QHostAddress m_destAddress;
    quint16 m_destPort;
    QString m_bindIP;
    quint16 m_bindPort;
    QMutex m_mutex;

    int m_width;
    int m_height;
    int m_fps;

    QElapsedTimer m_sendTimer;  // 记录上一帧发送时间
    qint64 m_lastSendTime = 0;  // 上一帧发送的时间戳（ms）
};

#endif // DATASENDER_H
